﻿using Castle.Windsor;
using Castle.Windsor.Installer;
using IceDog.DNF.Program.Castle_Windsor.CastleWindsor;
using System.Web.Mvc;
using System.Web.Routing;

namespace IceDog.DNF.Program.Castle_Windsor
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static IWindsorContainer _container;

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BootstrapContainer();
        }

        private static void BootstrapContainer()
        {
            _container = new WindsorContainer();
            _container.Install(FromAssembly.This());
            //生成Castle Windsor实例，赋值给自定义ControllerFactory的构造函数
            ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container.Kernel));
        }

        protected void Application_End()
        {
            //在Application结束时销毁该Castle Windsor实例
            _container.Dispose();
        }
    }
}
